#!/bin/bash

## loop xxx-terminal
terminal_exe=/usr/bin/gnome-terminal
if [ ! -x ${terminal_exe} ]; then
for i in `ls /usr/bin/*terminal`; do
    if [ -x ${i} ]; then
        terminal_exe=${i}
        ## look for the keyword "COMMAND"/"EXECUTE" in "--help"
        temp=`${terminal_exe} --help | tr a-z A-Z`
        if echo ${temp} | grep COMMAND > /dev/null ;then
            break
        elif echo ${temp} | grep EXECUTE > /dev/null ;then
            break
        fi
    fi
done
fi

## target
if [ -e $1 ]; then
    target_shell=$1
    chmod a+x ${target_shell}
else
    target_shell=/tmp/terminal_temp.sh
    echo "#!/bin/bash" > ${target_shell}
    echo "echo '$1'" >> ${target_shell}
    echo "sleep 2" >> ${target_shell}
    chmod 777 ${target_shell}
fi

## open a new terminal
if [ -x ${terminal_exe}  ]; then
    ## terminal operation
    case ${terminal_exe}  in
    *gnome-terminal) ## ubuntu
        ${terminal_exe}  -x ${target_shell} & ;;
    *deepin-terminal) ## uos
        ${terminal_exe}  -e ${target_shell} & ;;
    *lxterminal) ## pi
        ${terminal_exe}  -e ${target_shell} & ;;
    *mate-terminal) ## kylin
        ${terminal_exe}  -x ${target_shell} & ;;
    *) ## unknown
        ${terminal_exe}  -x ${target_shell} & ;;
    esac
fi
